1.6. Fixing the Script
Because you selected to
script for SQL Server 2008, the script includes some syntax and
statements that aren't supported in SQL Azure. Figure 5 shows some of the errors you see if you try to run the script as generated.
Another problem is that SQL Azure doesn't support heap tables. A heap table is one without a clustered index. SQL Azure currently supports only clustered tables.
You need to make some changes for your script to run under SQL Azure. Here's what to do:
Delete all instances of SET ANSI_NULLS ON.
Delete all instances of ON [PRIMARY].
Delete all instance of PAD_INDEX = OFF as well as ALLOW_ROW_LOCKS = ON and ALLOW_PAGE_LOCKS = ON.
In the Users table, modify the rowguid column, changing DEFAULT NEWSEQUENTIALID() to NULL.
In the stored procedure, remove the ENCRYPTION clause.
Add a clustered index to any heap tables.
ON [PRIMARY ] isn't needed because, SQL Azure hides all hardware-specific access and information. There is no concept of PRIMARY or file groups because disk space is handled by Microsoft, so this option isn't required.
According to SQL Server Books Online (BOL) you can remove the entire WITH clause that contains the table options. However, the only table options you really need to remove are those listed in step 3 (PAD_INDEX, ALLOW_ROW_LOCKS, and ALLOW_PAGE_LOCKS).
The NEWSEQUENTIALID()
function isn't supported in SQL Azure because there is no CLR support
in SQL Azure, and thus all CLR-based types aren't supported. The NEWSEQUENTIALID() return value is one of those types. Also, the ENCRYPTION option isn't supported because SQL Azure as a whole doesn't yet support encryption.
SQL
Azure doesn't support heap tables. Thus, you need to change any heap
table into a clustered table by adding a clustered index.
(Interestingly, if you execute one statement at a time, you can, in
fact, create a heap table. However, any inserts into that table fail.)
A word about the final item in the list. The syntax for defining a clustered index looks like this:
CREATE TABLE [dbo].[UserDocs]
(
[UserID] [int] NOT NULL,
[DocID] [int] NOT NULL
PRIMARY KEY CLUSTERED
(
[UserID], [DocID] ASC
)
)
One of the things
the SQL Azure documentation suggests, and which is listed earlier, is to
set the Convert UDDTs to Base Types property to True. This is because
user-defined types aren't supported in SQL Azure.
After you make the changes just described to your SQL script, it should look like the following:
/****** Object: Table [dbo].[Users] Script Date: 03/31/2010 23:39:20 ******/
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Users](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](50) NULL,
[NTUserName] [nvarchar](128) NULL,
[Domain] [nvarchar](50) NOT NULL,
[Intro] [nvarchar](100) NULL,
[Title] [nvarchar](50) NOT NULL,
[State] [nvarchar](10) NOT NULL,
[Country] [nvarchar](100) NULL,
[PWD] [varbinary](100) NULL,
[rowguid] [uniqueidentifier] NULL
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF)
)
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Users] ON
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (1, N'Herve Roggero', N'hroggero', N'PYNLOGIC',
N'Enterprise and Azure Architect; Speaker. Expert knowledge in C#. Prev. mgmt exp.',
N'Azure Architect', N'FL', N'US',
0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (2, N'Jim Mullis', N'jmullis', N'PYNLOGIC',
N'Expert in software development. C++; Oracle; SQL Server DBA', N'', N'FL', N'US', 0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
INSERT [dbo].[Users] ([ID], [Name], [NTUserName], [Domain], [Intro], [Title],
[State], [Country], [PWD])
VALUES (3, N'Scott Klein', N'sklein', N'',
N'Expert in software development. MVP SQL Server. Author. Speaker.',
N'Architect', N'FL', N'US', 0xE8F97FBA9104D1EA5047948E6DFB67FACD9F5B73)
SET IDENTITY_INSERT [dbo].[Users] OFF
/****** Object: StoredProcedure [dbo].[proc_CreateProfile]
Script Date: 03/31/2010 23:39:21 ******/
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[proc_CreateProfile]
@uid [nvarchar](50),
@pwd [nvarchar](50),
@name [nvarchar](50),
@title [nvarchar](50),
@country [nvarchar](50),
@state [nvarchar](20),
@rowguid uniqueidentifier
WITH RECOMPILE
AS
DECLARE @password varbinary(100)
SET @password = HASHBYTES('sha1', @pwd)
-- Make sure the UID is not already taken...
IF (Exists(SELECT TOP 1 * FROM Users WHERE NTUserName = @uid))
BEGIN
RAISERROR(N'0x001 - User ID already in use', 16, 1)
END
ELSE
BEGIN
INSERT INTO Users
(Name, NTUserName, Domain, Intro, Title, State, Country, PWD, rowguid)
VALUES
(@name, @uid, '', '', @title, @state, @country, @password, @rowguid)
END
GO
Now that you've made the necessary corrections, you're ready to create your objects in a SQL Azure database.
1.7. Executing the Script Against an Azure Database
You don't have a SQL Azure database to run the script against, so let's create one now:
Connect to your SQL Azure instance, making sure you're connecting to the master database.
Open a new query window, and use the syntax discussed in this article to create your SQL Azure database. Name it TechBio, because this is the name the examples use throughout this article.
Click
over to the generated script. This query window is currently connected
to your local SQL instance, so you need to change it to your SQL Azure
instance and the database you just created. Right-click anywhere in the
script, and select Connection → Change Connection from the context menu.
In
the Connect to Database Engine dialog, enter the information for your
SQL Azure instance, and enter the name of the database you just created
on the Connection Properties tab.
You now have your script, a
database, and a connection to that database. Click the Execute button.
Your script should run and create the tables, procedures, and data in
your SQL Azure Database.
The SQL Server Generate and
Publish Script wizard is a great way to start understanding the required
changes that need to be made when migrating to SQL Azure. With this
foundation, let's discuss one of the other options, SQL Server
Integration Services.